package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址
 * @author Fat
 */
@Api(tags = "用户地址")
@RequestMapping("/user/addressBook")
@RestController("UserAddressBookController")
public class AddressBookController {

    private static final Logger log = LoggerFactory.getLogger(AddressBookController.class);

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 保存地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "保存地址")
    @PostMapping
    public Result saveAddressBook(@RequestBody AddressBook addressBook){
        log.info("保存地址：{}",addressBook);
        addressBookService.saveAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 查询地址列表
     * @return
     */
    @ApiOperation(value = "查询地址列表")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询地址列表");
        // 查询地址列表
        List<AddressBook> list = addressBookService.queryByUserId(BaseContext.getCurrentId());
        return Result.success(list);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> queryById(@PathVariable("id") Long id){
        log.info("根据id查询地址");
        AddressBook addressBook = addressBookService.queryById(id);
        return Result.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "更新地址")
    @PutMapping
    public Result updateByAddressBook(@RequestBody AddressBook addressBook){
        log.info("更新地址：{}",addressBook);
        addressBookService.updateByAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @ApiOperation(value = "删除地址")
    @DeleteMapping
    public Result deleteAddressBook(@RequestParam Integer id){
        log.info("根据id删除地址：{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }


    /**
     * 更新默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "更新默认地址")
    @PutMapping("/default")
    public Result updateDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("更新默认地址：{}",addressBook);
        addressBookService.updateDefaultAddress(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation(value = "查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> queryDefaultAddress(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.queryDefaultAddress(BaseContext.getCurrentId());
        return Result.success(addressBook);
    }

}
